#!/bin/sh
#
# Script to do a stack trace of a Susan daemon/program
# and create a core-file for postmortem debugging.
#
# We attempt to attach to running program
#
# Arguments to this script are
#  $1 = path to executable
#  $2 = main pid of running program to be traced back.
#  $3 = working directory
#
PNAME="`basename $1`"
WD="$3"

GCORE="@GCORE@"
GDB="@GDB@"
DBX="@DBX@"
MDB="@MDB@"

#
# Try to generate a core dump for postmortem analyzing.
#
POSTMORTEM="NONE"
if [ ! -z "${GCORE}" -a -x "${GCORE}" ]; then
   POSTMORTEM="${WD}/${PNAME}.core"
   ${GCORE} -o ${POSTMORTEM} $2 > /dev/null 2>&1
   POSTMORTEM="${POSTMORTEM}.$2"
   echo "Created ${POSTMORTEM} for doing postmortem debugging" > ${WD}/susan.$2.traceback
else
   which gcore > /dev/null 2>&1 && GCORE="`which gcore`" || GCORE=''
   if [ ! -z "${GCORE}" -a -x "${GCORE}" ]; then
      POSTMORTEM="${WD}/${PNAME}.core"
      ${GCORE} -o ${POSTMORTEM} $2 > /dev/null 2>&1
      POSTMORTEM="${POSTMORTEM}.$2"
      echo "Created ${POSTMORTEM} for doing postmortem debugging" > ${WD}/susan.$2.traceback
   fi
fi

#
# Try to find out what debugger is available on this platform.
#
if [ ! -z "${DBX}" -a -x "${DBX}" ]; then
   DEBUGGER="DBX"
elif [ ! -z "${GDB}" -a -x "${GDB}"  ]; then
   DEBUGGER="GDB"
elif [ ! -z "${MDB}" -a -x "${MDB}"  ]; then
   DEBUGGER="MDB"
else
   #
   # If we fail to find the configured debugger do a last try to find it on the PATH.
   #
   which gdb > /dev/null 2>&1 && GDB="`which gdb`" || GDB=''
   which dbx > /dev/null 2>&1 && DBX="`which dbx`" || DBX=''
   which mdb > /dev/null 2>&1 && MDB="`which mdb`" || MDB=''

   if [ ! -z "${DBX}" -a -x "${DBX}" ]; then
      DEBUGGER="DBX"
   elif [ ! -z "${GDB}" -a -x "${GDB}"  ]; then
      DEBUGGER="GDB"
   elif [ ! -z "${MDB}" -a -x "${MDB}"  ]; then
      DEBUGGER="MDB"
   else
      DEBUGGER="NONE"
   fi
fi

#
# Try to run a debugger on the crashing program or the generated postmortem dump for getting a strack trace.
#
case ${DEBUGGER} in
   DBX)
      if [ ${POSTMORTEM} != NONE -a -s ${POSTMORTEM} ]; then
         ${DBX} $1 ${POSTMORTEM} < @scriptdir@/btraceback.dbx >> ${WD}/susan.$2.traceback 2>&1
      else
         ${DBX} $1 $2 < @scriptdir@/btraceback.dbx >> ${WD}/susan.$2.traceback 2>&1
      fi
      ;;
   GDB)
      if [ ${POSTMORTEM} != NONE -a -s ${POSTMORTEM} ]; then
         ${GDB} -quiet -batch -x @scriptdir@/btraceback.gdb $1 ${POSTMORTEM} >> ${WD}/susan.$2.traceback 2>&1
      else
         ${GDB} -quiet -batch -x @scriptdir@/btraceback.gdb $1 $2 >> ${WD}/susan.$2.traceback 2>&1
      fi
      ;;
   MDB)
      if [ ${POSTMORTEM} != NONE -a -s ${POSTMORTEM} ]; then
         ${MDB} -u $1 ${POSTMORTEM} < @scriptdir@/btraceback.mdb >> ${WD}/susan.$2.traceback 2>&1
      else
         ${MDB} -u -p $2 < @scriptdir@/btraceback.mdb >> ${WD}/susan.$2.traceback 2>&1
      fi
      ;;
   NONE)
      echo "No debugger available on this platform," \
           " please install one to get proper stack traces" >> ${WD}/susan.$2.traceback
      ;;
esac

#
# Send the stack trace info to someone for analyzing or at least letting someone know we crashed.
#
PNAME="${PNAME} on `hostname`"
cat ${WD}/susan.$2.traceback | \
@sbindir@/bsmtp -h @smtp_host@ -f @dump_email@ -s "Susan ${DEBUGGER} traceback of ${PNAME}" @dump_email@
